﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Ria.ApplicationServices;
using CARE;
using CARE.Web;
using CARE.Web.Services;
using CARE.GraphProviderServiceReference;

namespace CARE
{
    public partial class Source : UserControl
    {
        private GraphProviderServiceClient gp;

        private IWizardButtonController wizardButtonController;

        private GenerateSocialNetworkWindow generator;

        public Source(IWizardButtonController sw)
        {
            // Required to initialize variables
            InitializeComponent();
            gp = new GraphProviderServiceClient();
            gp.GetGraphFromGraphMLCompleted += new EventHandler<GetGraphFromGraphMLCompletedEventArgs>(gp_GetGraphFromGraphMLCompleted);
            wizardButtonController = sw;
        }

        private void Import_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            String graphML;
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "XML Files (*.xml)|*.xml|GraphML Files (*.graphml)|*.graphml";
            Nullable<Boolean> result = dialog.ShowDialog();
            if (result.Value == true)
            {
                graphML = dialog.File.OpenText().ReadToEnd();
                InoculationParameters.getInstance().SocialNetworkGraphML = graphML;
                gp.GetGraphFromGraphMLAsync(graphML);
            }
        }

        void gp_GetGraphFromGraphMLCompleted(object sender, GetGraphFromGraphMLCompletedEventArgs e)
        {
            if (wizardButtonController is SpreadVizualization)
            {
                SimulationParameters simParameters = SimulationParameters.getInstance();
                simParameters.SocialNetworkGraph = e.Result as Graph;
            }
            else
            {
                InoculationParameters inocParameters = InoculationParameters.getInstance();
                inocParameters.SocialNetworkGraph = e.Result as Graph;
            }

            wizardButtonController.EnableNext();
        }

        private void Generate_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            generator = new GenerateSocialNetworkWindow(false);
            generator.wizardButtonController = wizardButtonController;
            generator.Show();
        }

        private void Database_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            var databasesource = new Views.DatabaseSocialNetworkWindow();
            databasesource.Closed += new EventHandler(databasesource_Closed);
            databasesource.Show();
        }

        void databasesource_Closed(object sender, EventArgs e)
        {
            //TODO: convert net to graph
            Views.DatabaseSocialNetworkWindow databasesource = sender as Views.DatabaseSocialNetworkWindow;
            if (databasesource.DialogResult.Value)
            {
                //todo: do this by interface
                if (wizardButtonController is SpreadVizualization)
                {
                    SimulationParameters simParameters = SimulationParameters.getInstance();
                    simParameters.SocialNetworkGraphML = databasesource.SocialNetwork;
                }
                else
                {
                    InoculationParameters inocParameters = InoculationParameters.getInstance();
                    inocParameters.SocialNetworkGraphML = databasesource.SocialNetwork;
                }
                wizardButtonController.EnableNext();
            }
        }
    }
}